package com.study.zookeeper.case3;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

public class DistributeLockeTest {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        final DistributeLocke locke1 = new DistributeLocke();
        final DistributeLocke locke2 = new DistributeLocke();
        new Thread(new Runnable(){
            @Override
            public void run() {
                    locke1.lock();
                    System.out.println(Thread.currentThread().getName()+"加锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                locke1.unlock();
                System.out.println(Thread.currentThread().getName()+"解锁");
            }
        },"thread1").start();

        new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    locke2.lock();

                System.out.println(Thread.currentThread().getName()+"加锁");
                    Thread.sleep(1000);
                locke2.unlock();
                System.out.println(Thread.currentThread().getName()+"解锁");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"thread2").start();

    }
    /*public static void main(String[] args) throws InterruptedException, IOException, KeeperException {

        final  DistributeLocke lock1 = new DistributeLocke();
        final  DistributeLocke lock2 = new DistributeLocke();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock1.lock();
                    System.out.println("线程1 启动，获取到锁");
                    Thread.sleep(5 * 1000);

                    lock1.unlock();
                    System.out.println("线程1 释放锁");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    lock2.lock();
                    System.out.println("线程2 启动，获取到锁");
                    Thread.sleep(5 * 1000);

                    lock2.unlock();
                    System.out.println("线程2 释放锁");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }*/
}
